﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TwinCAT.Ads;
namespace WpfADSIOReader1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private TcAdsSymbolInfoLoader symbolLoader;
        private TcAdsClient adsClient;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                adsClient = new TcAdsClient();

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            treeViewSymbols.Items.Clear();
            //check adr info
            AmsAddress serverAddress = null;
            try
            {
                // check if port is a hex value
                if (txtport.Text.StartsWith(("0x")) || txtport.Text.StartsWith(("0X")))
                {
                    string sHexValue = txtport.Text.Substring(2);
                    serverAddress = new AmsAddress(this.txtnetID.Text, Int32.Parse(sHexValue, System.Globalization.NumberStyles.HexNumber));
                }
                // interpret as dec value
                else
                {
                    serverAddress = new AmsAddress(txtnetID.Text, Int32.Parse(txtport.Text));
                }

            }
            catch
            {
                MessageBox.Show("Invalid AMS NetId " + txtnetID.Text + " or Ams port " + txtport.Text + "!");
                return;
            }
            //connect
            try
            {
                //orig for just 127.0...: adsClient.Connect(Convert.ToInt32(tbAdsPort.Text));
                adsClient.Connect(serverAddress.NetId, serverAddress.Port);
                symbolLoader = adsClient.CreateSymbolInfoLoader();

               
                    TcAdsSymbolInfo symbol = symbolLoader.GetFirstSymbol(true);
                    while (symbol != null)
                    {
                        treeViewSymbols.Items.Add(CreateNewItem(symbol));
                        symbol = symbol.NextSymbol;
                    }
  
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private TreeViewItem CreateNewItem(TcAdsSymbolInfo symbol)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = symbol.Name;
            TcAdsSymbolInfo subSymbol = symbol.FirstSubSymbol;
            while (subSymbol!=null)
            {
                item.Items.Add(CreateNewItem(subSymbol));
                subSymbol = subSymbol.NextSymbol;
            }
            return item;
        }
        private void treeViewSymbols_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem treeViewItem= (TreeViewItem)this.treeViewSymbols.SelectedItem;
            string selStr =  treeViewItem.Header.ToString();
            TcAdsSymbolInfo info = null;
            if (selStr!=null)
            {
               info= symbolLoader.FindSymbol(selStr);
            }
            if (info!=null)
            {
                txtName.Text = info.Name;
                txtSize.Text = info.Size.ToString();
                ITcAdsSymbol symbol = adsClient.ReadSymbolInfo(selStr);
                txtDatatype.Text = symbol.GetType().ToString();
                try
                {
                    txtValue.Text = adsClient.ReadSymbol(symbol).ToString();
                }
                catch (AdsDatatypeNotSupportedException err)
                {

                    txtValue.Text = err.ToString();
                }
                
            }
           
            
        }
    }
}
